﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using TanWeb.Models.Entities.Infrastructure;
using TanWeb.Helpers;

namespace TanWeb.Models.Entities
{
    [Table("SubItemGroup")]
    public class Image : Entity
    {
        [MaxLength(8)]
        [Column("GroupId", Order = 1)]
        public int GroupId { set; get; }

        [MaxLength(150)]
        [Column("SubGroupId", Order = 2)]
        public int SubGroupId { set; get; }

        [MaxLength(150)]
        [Column("ProductId", Order = 3)]
        public int ProductId { set; get; }

        [MaxLength(150)]
        [Column("ImageName", Order = 4)]
        public string ImageName { set; get; }

        [MaxLength(1)]
        [Column("del_flg", Order = 5)]
        public int del_flg { set; get; }

        public string TempImagePath { set; get; }

        public void SetTempImagePath()
        {
            if (this.ImageName == null || this.ImageName == string.Empty)
            {
                ImageName = string.Empty;
                return;
            }

            string path = Constants.PATH_UPLOAD_IMAGE.Replace("..", "") + "/" + this.ImageName;
            string imagePath = System.Web.Hosting.HostingEnvironment.MapPath(path);
            string type = this.id == 1 ? Constants.TYPE_IMAGE_NORMAL : Constants.TYPE_IMAGE_MINIMUM;
            System.Drawing.Image image = Utils.GetResizedImage(imagePath, type);
            TempImagePath = Utils.GetImageUrl(image);
        }
    }
}